package com.zfc.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

/**
 * JWT工具类
 */
public class JWTUtil {
    static final private String salt = "123#";

    public static String getToken(Map<String, String> map){
        JWTCreator.Builder builder = JWT.create();
        map.forEach((k,v) -> {
            builder.withClaim(k, v);
        });
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, 7); //有效期7天

        String token = builder.withExpiresAt(calendar.getTime())
                .sign(Algorithm.HMAC256(salt));
        return token;
    }

    public static DecodedJWT vertifyToken(String token){
        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(salt)).build();
        DecodedJWT verify = jwtVerifier.verify(token);
        return verify;
    }

}
